package com.xiaren.ui.edit;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

import com.xiaren.biz.PublicFunction;
import com.xiaren.common.ObjectConstant;

/**
 * 在编辑文本的选项卡中，鼠标的右键窗体<br/>
 * 单例类
 *
 * @author xiaren
 * @主页 http://www.leaf123.cn/
 * @邮箱 xia5523@qq.com
 */
public class EditRightWindow extends JPopupMenu implements ActionListener {

	JMenuItem close = new JMenuItem("Close");
	JMenuItem closeother = new JMenuItem("Close other");
	JMenuItem closeall = new JMenuItem("Close all");

	public EditRightWindow() {
		super();
		init();
	}

	void init() {
		this.add(close);
		// this.add(closeother);
		this.add(closeall);

		close.addActionListener(this);
		// closeother.addActionListener(this);
		closeall.addActionListener(this);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == close) {
			FileEdit edit = (FileEdit) ObjectConstant.mainEdit
					.getSelectedComponent();
			if (!edit.isSave) {
				int result = JOptionPane.showConfirmDialog(null,
						"The text did not save ,want to save the text ?",
						" warn ", JOptionPane.ERROR_MESSAGE);

				if (result == JOptionPane.OK_OPTION) {
					edit.saveMySelf();
				} else if (result == -1) {// 点击xx，不做任何操作
					return;
				}
			}
			ObjectConstant.mainEdit.remove(edit);
		} else if (e.getSource() == closeother) {
			// for (int i = 0; i < ObjectConstant.mainEdit.getTabCount(); i++) {
			// if (i != ObjectConstant.mainEdit.getSelectedIndex()) {
			// ObjectConstant.mainEdit.remove(i);
			// ObjectConstant.mainEdit.listEdit
			// .remove(((FileEdit) ObjectConstant.mainEdit
			// .getComponent(i)).treeNote.treeNodeId);
			//
			//
			// }
			// }

		} else if (e.getSource() == closeall) {
			if (PublicFunction.fileNotSave()) {
				int result = JOptionPane.showConfirmDialog(null,
						"Here is text didn't save, Whether to save all ?",
						" warn ", JOptionPane.ERROR_MESSAGE);
				if (result == JOptionPane.OK_OPTION) {
					PublicFunction.saveAllFile();
				} else if (result == -1) {
					return;
				}
			}
			ObjectConstant.mainEdit.removeAll();

		}
	}

}
